package lesson02;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

class MyFrame extends Frame {
    ArrayList points;
    //画画需要画笔，需要监听鼠标当前的位置，需要用集合来存储这个点
    public MyFrame(String title){
        super(title);
        setBounds(200, 200,400,400);
        //鼠标监听器，正对这个接口
        points = new ArrayList<>();
        this.addMouseListener(new MyMouseListener());
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画监听鼠标事件
        Iterator iterator = points.iterator();
        while(iterator.hasNext()) {
            Point p = (Point) iterator.next();
            g.setColor(Color.blue);
            g.fillOval(p.x,p.y,10,10);
        }
    }
    public void addPoint(Point point){
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter{
        //鼠标，按下，弹起，按住不放

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame=(MyFrame) e.getSource();
            //这个我们点击的时候，就会在界面上产生一个点
            //这个点就是鼠标的点
//            new Point(e.getX(),e.getY());
            frame.addPoint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都要重新画一次
            frame.repaint();
        }
    }
}
